#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

pthread_mutex_t mutex;
void *hello(void *greet){
    char *str = (char *) greet;
    printf("Hello, %s\n", str);
    return NULL;
}

void* more(void *n){
    int *x = (int *) n;
    int i  = 0;
    for(;i<1000;i++){
        pthread_mutex_lock(&mutex);
        *x = *x + 1;
        pthread_mutex_unlock(&mutex);
    } 
    return NULL;
}

void* less(void *n){
    int *x = (int *) n;
    int i  = 0;
    for(;i<1000;i++){
        pthread_mutex_lock(&mutex);
        *x = *x - 1;
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}

int main(int argc, char *argv[]){
    /* TODO: complete main function */
    pthread_t x,y;

    int my_int = 0;
    pthread_mutex_init(&mutex, NULL);
    pthread_create(&x,NULL,more, &my_int);
    pthread_create(&y,NULL,less, &my_int);
    
    pthread_join(x, NULL);
    pthread_join(y, NULL);
    if(my_int != 0)
        printf("%d\n", my_int);
    return 0;
}
